# vim:set ft=dockerfile:
FROM walg-func-test-base

ARG WALG_REPO=${GOPATH}/src/github.com/wal-g/wal-g
ARG REDIS_VERSION=6.0.8
ENV REDIS_DOWNLOAD_URL http://download.redis.io/releases/redis-$REDIS_VERSION.tar.gz

ENV TMP_DIR /var/tmp/wal-g
ENV WALG_CONF_DIR /etc/wal-g/

ENV USER redis
ENV GROUP redis
ENV USERGROUP $USER:$GROUP
ENV HOME /home/$USER

# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
RUN groupadd -r -g 999 $USER && useradd -r -g $GROUP -u 999 $USER

RUN set -eux; \
	\
	savedAptMark="$(apt-mark showmanual)"; \
	apt-get update; \
	apt-get install -y --no-install-recommends \
		ca-certificates \
		wget \
		\
		gcc \
		libc6-dev \
		make \
	; \
	rm -rf /var/lib/apt/lists/*; \
	\
	wget -O redis.tar.gz "$REDIS_DOWNLOAD_URL"; \
	mkdir -p /usr/src/redis; \
	tar -xzf redis.tar.gz -C /usr/src/redis --strip-components=1; \
	rm redis.tar.gz; \
	\
## disable Redis protected mode [1] as it is unnecessary in context of Docker
## (ports are not automatically exposed when running inside Docker, but rather explicitly by specifying -p / -P)
## [1]: https://github.com/antirez/redis/commit/edd4d555df57dc84265fdfb4ef59a4678832f6da
#	grep -q '^#define CONFIG_DEFAULT_PROTECTED_MODE 1$' /usr/src/redis/src/server.h; \
#	sed -ri 's!^(#define CONFIG_DEFAULT_PROTECTED_MODE) 1$!\1 0!' /usr/src/redis/src/server.h; \
#	grep -q '^#define CONFIG_DEFAULT_PROTECTED_MODE 0$' /usr/src/redis/src/server.h; \
## for future reference, we modify this directly in the source instead of just supplying a default configuration flag because apparently "if you specify any argument to redis-server, [it assumes] you are going to specify everything"
## see also https://github.com/docker-library/redis/issues/4#issuecomment-50780840
## (more exactly, this makes sure the default behavior of "save on SIGTERM" stays functional by default)
	\
	make -C /usr/src/redis -j "$(nproc)"; \
	make -C /usr/src/redis install; \
	\
# TODO https://github.com/antirez/redis/pull/3494 (deduplicate "redis-server" copies)
	serverMd5="$(md5sum /usr/local/bin/redis-server | cut -d' ' -f1)"; export serverMd5; \
	find /usr/local/bin/redis* -maxdepth 0 \
		-type f -not -name redis-server \
		-exec sh -eux -c ' \
			md5="$(md5sum "$1" | cut -d" " -f1)"; \
			test "$md5" = "$serverMd5"; \
		' -- '{}' ';' \
		-exec ln -svfT 'redis-server' '{}' ';' \
	; \
	\
	rm -r /usr/src/redis; \
	\
	apt-mark auto '.*' > /dev/null; \
	[ -z "$savedAptMark" ] || apt-mark manual $savedAptMark > /dev/null; \
	apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
	\
	redis-cli --version; \
	redis-server --version

RUN mkdir -p /data && chown $USERGROUP /data
VOLUME /data
WORKDIR /data

RUN apt-get update -qq && \
    apt-get install -y apt-transport-https && \
    mkdir -p /etc/apt/sources.list.d && \
    apt-get update -qq && \
    apt-key update && \
    rm -rf /var/lib/apt/lists/* /var/cache/debconf && \
    apt-get clean

RUN mkdir -p /var/log/wal-g/ ${TMP_DIR} && \
    ln --force -s /config/supervisor/conf.d/redis.conf /etc/supervisor/conf.d/redis.conf && \
    mkdir -p ${WALG_REPO} ${WALG_CONF_DIR} && \
    mkdir -p $HOME/.gnupg && touch $HOME/.gnupg/gpg.conf && \
    chmod -R 700 $HOME/.gnupg && chown $USERGROUP -R $HOME/.gnupg && \
    ln --force -s /config/wal-g-${REDIS_VERSION}.json ${WALG_CONF_DIR}/wal-g.json && \
    ln --force -s /config/gpg-key.armor ${WALG_CONF_DIR}/gpg-key.armor

COPY staging/wal-g ${WALG_REPO}/
COPY staging/wal-g/redis_cli.sh /usr/bin/redis_cli.sh

WORKDIR ${WALG_REPO}

RUN make link_external_deps
RUN make BUILD_ARGS=-race redis_build && \
    cp main/redis/wal-g /usr/bin/wal-g
